package com.netease.nim.demo.main.helper;

import logic.bean.TeamMemberX;
import android.text.TextUtils;

import com.netease.nim.demo.contact.cache.ContactDataCache;
import com.netease.nim.demo.contact.cache.TeamDataCache;
import com.netease.nim.demo.contact.cache.TeamMemberInfoCache;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.constant.SystemMessageStatus;
import com.netease.nimlib.sdk.msg.constant.SystemMessageType;
import com.netease.nimlib.sdk.msg.model.SystemMessage;
import com.netease.nimlib.sdk.team.model.Team;


/**
 * Created by huangjun on 2015/4/9.
 */
public class MessageHelper
{
	public static String getName(String uid, SessionTypeEnum sessionType) {
		if (sessionType == SessionTypeEnum.P2P)
		{
			TeamMemberX bean = TeamMemberInfoCache.getInstance().getTeammMemberInfo(uid);
			if (bean != null)
			{
				if (!TextUtils.isEmpty(bean.getStaffName()))
				{
					return bean.getStaffName();
				}
				else if (!TextUtils.isEmpty(bean.getNickName()))
				{
					return bean.getNickName();
				}
				else
				{
					return bean.getAccount();
				}
			}
			return ContactDataCache.getInstance().getBuddyDisplayName(uid);
		}
		else if (sessionType == SessionTypeEnum.Team) { return TeamDataCache.getInstance().getTeamName(uid); }
		return uid;
	}
	
	public static String getVerifyNotificaiontText(SystemMessage message) {
		StringBuilder sb = new StringBuilder();
		String fromAccount = ContactDataCache.getInstance().getBuddyDisplayNameYou(message.getFromAccount());
		Team team = TeamDataCache.getInstance().getTeamById(message.getTargetId());
		if (team == null && message.getAttachObject() instanceof Team)
		{
			team = (Team) message.getAttachObject();
		}
		String teamName = team == null ? message.getTargetId() : team.getName();
		
		if (message.getType() == SystemMessageType.TeamInvite)
		{
			sb.append("邀请").append("你").append("加入群 ").append(teamName);
		}
		else if (message.getType() == SystemMessageType.DeclineTeamInvite)
		{
			sb.append(fromAccount).append("拒绝了群 ").append(teamName).append(" 邀请");
		}
		else if (message.getType() == SystemMessageType.ApplyJoinTeam)
		{
			sb.append("申请加入群 ").append(teamName);
		}
		else if (message.getType() == SystemMessageType.RejectTeamApply)
		{
			sb.append(fromAccount).append("拒绝了你加入群 ").append(teamName).append("的申请");
		}
		
		return sb.toString();
	}
	
	public static boolean isVerifyNotificationNeedDeal(SystemMessage message) {
		if (message.getType() == SystemMessageType.TeamInvite || message.getType() == SystemMessageType.ApplyJoinTeam)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public static String getVerifyNotificationDealResult(SystemMessage message) {
		if (message.getStatus() == SystemMessageStatus.passed)
		{
			return "已同意";
		}
		else if (message.getStatus() == SystemMessageStatus.declined)
		{
			return "已拒绝";
		}
		else if (message.getStatus() == SystemMessageStatus.ignored)
		{
			return "已忽略";
		}
		else if (message.getStatus() == SystemMessageStatus.expired)
		{
			return "已过期";
		}
		else
		{
			return "未处理";
		}
	}
}
